//
//  WYStatusView.swift
//  weibo
//
//  Created by 王玉东 on 16/4/13.
//  Copyright © 2016年 wyd. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
let StatusCellMargin:CGFloat = 8

class WYStatusView: UIView {
    
    var bottomCons:Constraint?
    var statusModel :WYStatusModel?{
        didSet{
        
            iconView.sd_setImageWithURL(statusModel?.headImageURL)
            nameLable.text = statusModel?.status?.user?.name
            

            mbRankImageView.image = statusModel?.mbImage
            verifiedImageView.image = statusModel?.verifiedImage
            
            //timeLable.text = statusModel?.status?.created_at
            timeLable.text = statusModel?.time
            sourceLable.text = statusModel?.source
            
            
            contentLable.text = statusModel?.status?.text
            
            //pictureView.imageURLs = statusModel?.status?.pic_urls
            
           // contentLable.numberOfLines = 0
            //配图视图设置数据源
            self.bottomCons?.uninstall()
            if let urls = statusModel?.status?.pic_urls where urls.count > 0{
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源
                pictureView.imageURLs = urls
                pictureView.hidden = false
            
            }else{
            
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLable.snp_bottom).offset(StatusCellMargin).constraint
                })
                pictureView.imageURLs = nil
                pictureView.hidden = true
            
            }
        
        
        }
    
    }
private lazy var pictureView: WYStatusPictureView = WYStatusPictureView()
    
 private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))

 private lazy var mbRankImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
 private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))

private lazy var nameLable: UILabel = UILabel(text: "同学们", fontSize: 15, textColor: UIColor.darkGrayColor())
    
private lazy var timeLable: UILabel = UILabel(text: "15:15", fontSize: 12, textColor: UIColor.darkGrayColor())
    
private lazy var sourceLable: UILabel = UILabel(text: "隔壁老王", fontSize: 12, textColor: UIColor.darkGrayColor())
    
    
private lazy var contentLable: UILabel = UILabel (text: "我没有女朋友", fontSize: 12, textColor: UIColor.darkGrayColor(), margin: 8)

private lazy var sepView:UIView = UIView()

  
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        //backgroundColor = UIColor.redColor()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
    
        backgroundColor = UIColor.redColor()
        iconView.cornerRadius = 20
        addSubview(sepView)
        sepView.backgroundColor = UIColor.blueColor()
        addSubview(iconView)
        addSubview(nameLable)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(contentLable)
        addSubview(pictureView)
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(15)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
           //make.left.top.equalTo(self).offset(StatusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLable.snp_top)
            make.left.equalTo(nameLable.snp_right).offset(StatusCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLable.snp_right).offset(StatusCellMargin)
        }
        
        contentLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLable.snp_left)
            make.top.equalTo(contentLable.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(contentLable.snp_bottom).offset(StatusCellMargin)
            //make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    
    }
    
    
    

}
